package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.qmyx.admin.dao.SelectCommentDao;
import com.ytjj.qmyx.admin.dao.SelectExamDao;
import com.ytjj.qmyx.admin.dao.SelectExamProposalDao;
import com.ytjj.qmyx.admin.dao.SelectRelationDao;
import com.ytjj.qmyx.admin.model.Admin;
import com.ytjj.qmyx.admin.model.SelectExamProposal;
import com.ytjj.qmyx.admin.model.SelectExamPropose;
import com.ytjj.qmyx.admin.model.request.SelectExamRequest;
import com.ytjj.qmyx.admin.model.request.exam.SelectExamProposeRequest;
import com.ytjj.qmyx.admin.model.response.SelectExamResponse;
import com.ytjj.qmyx.admin.model.response.exam.SelectExamProposeResponse;
import com.ytjj.qmyx.admin.service.MemberService;
import com.ytjj.qmyx.admin.service.SelectExamService;
import com.ytjj.qmyx.mall.model.SelectExam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 *
 * @Version: 1.0
 */
@Service
public class SelectExamServiceImpl implements SelectExamService {

	@Resource
	private SelectExamDao selectExamDao;

	@Autowired
	private MemberService memberService;

	@Resource
	private SelectRelationDao selectRelationDao;

	@Resource
	private SelectCommentDao selectCommentDao;

	@Resource
	private SelectExamProposalDao selectExamProposalDao;

	@Override
	public CommonPage<SelectExamResponse> selectList(SelectExamRequest request) {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<SelectExamResponse> pageList = selectExamDao.selectList(request);
		if (!CollectionUtils.isEmpty(pageList)) {
			for (SelectExamResponse response : pageList) {

				// 查询是否存在中间表消息，不存在就创建
				if(selectRelationDao.isExist(response.getId()) <= 0) {
					selectRelationDao.createSelRela(response.getId());
					selectRelationDao.createAdminRela(response.getId());
				}
				Integer status = 1;
				System.out.println(memberService.getAdmin().toString());
				if(memberService.getAdmin().getId() != 32) {
					status = selectRelationDao.getStatus(response.getId(), 2);
				} else {
					status = selectRelationDao.getStatus(response.getId(), 1);
				}
				if(status == 1) {
					response.setMessageFlag(0);
				} else {
					response.setMessageFlag(1);
				}

				// 消息总数
				response.setMessageCount(selectCommentDao.getMessageCount(response.getId()));
			}
		}
		return CommonPage.restPage(pageList);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Integer insertSelect(SelectExam selectExam) {
		selectExam.setSelectorId(memberService.getAdmin().getId());
		selectExam.setSelector(memberService.getAdmin().getName());

		Integer count = selectExamDao.insertSelect(selectExam);
		// 辅助操作
		selectRelationDao.createSelRela(selectExam.getId());
		selectRelationDao.createAdminRela(selectExam.getId());
		return count;
	}

	@Override
	public Integer startExam(Integer id, Integer startStatus, String startOpinion) {
		Integer startExamId = memberService.getAdmin().getId();
		String startExamName = memberService.getAdmin().getName();
		return selectExamDao.startExam(id, startExamId, startExamName, startStatus, startOpinion);
	}

	@Override
	public Integer endExam(Integer id, Integer endStatus, String endOpinion) {
		if(!StringUtils.isEmpty(endOpinion)) {
			selectCommentDao.insertComment(id, memberService.getAdmin().getId(), endOpinion);
			// 终审核只能是老板 发布评论后需做消息红点提醒
			selectRelationDao.updateStatus(id, 2, 0);
		}
		return selectExamDao.endExam(id, endStatus, endOpinion);
	}

	@Override
	public Integer updateSelect(SelectExam selectExam) {
		if(selectExam.getIsExist() == 0) {
			selectExam.setProductId(null);
		}
		return selectExamDao.updateSelect(selectExam);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void propose(SelectExamProposeRequest request) {
		Admin admin = memberService.getAdmin();
		// 保存终审建议
		savePropose(request.getPropose(), request.getSelectExamId(), admin.getId(), admin.getName());
		//更改终审状态
		this.endExam(request.getSelectExamId(),request.getEndStatus(),request.getEndOpinion());
	}

	@Override
	public SelectExamProposeResponse getPropose(Integer selectExamId) {
		SelectExam selectExam = selectExamDao.findById(selectExamId);
		SelectExamProposeResponse response = new SelectExamProposeResponse();
		response.setSelectExamId(selectExamId);
		response.setEndStatus(selectExam.getEndStatus());
		response.setEndOpinion(selectExam.getEndOpinion());
		if(selectExam.getEndStatus().equals(1) || selectExam.getEndStatus().equals(3)) {
			SelectExamPropose propose = selectCommentDao.getPropose(selectExamId);
			response.setPropose(propose);
		}
		return response;
	}

	private void savePropose(SelectExamPropose propose,Integer selectExamId,Integer userId, String userName) {
		if (propose.getType() != null) {
			Integer id = selectExamDao.findByExamIdAndType(selectExamId);
			SelectExamProposal selectExamProposal = new SelectExamProposal();
			BeanUtils.copyProperties(propose, selectExamProposal);
			selectExamProposal.setSelectExamId(selectExamId);
			selectExamProposal.setLastUser(userName);
			selectExamProposal.setLastUserId(userId);
			selectExamProposal.setType(propose.getType());
			if(id == null) {
				selectExamProposalDao.insertProposal(selectExamProposal);
			}else {
				selectExamProposal.setId(id);
				selectExamProposalDao.updateProposal(selectExamProposal);
			}
		} else {
			// 先查询是否存在选品审核中间信息
			Integer isExist = selectExamProposalDao.isExist(selectExamId);
			if(isExist > 0) {
				// 清空中间信息
				selectExamProposalDao.clearPropose(selectExamId);
			}
		}
	}

	@Override
	public void updateExpressNo(Integer selectExamId, String sampleExpressNo) {
		selectExamProposalDao.updateExpressNo(selectExamId, sampleExpressNo);
	}

	@Override
	public void updateContentStatus(Integer selectExamId) {
		selectExamProposalDao.updateContentStatus(selectExamId);
	}
}
